// pages/uploadImages/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isPending: false, // 是否正在上传
    maxVal: 0, // 上传图片多少张
    curVal: 0, // 当前上传到第几张
    imgUrlTempList: [], // 图片路径列表
    logList: [], // 日志列表 上传几张、耗时多少
    uploadType: 0, // 上传方式 0 /1 
  },
  uploadImg() {
    // 让用户选择一张图片
    wx.chooseImage({
      count: 10,
      success: chooseResult => {
        // 将图片上传至云存储空间
        const _list = chooseResult.tempFilePaths;
        if (this.data.uploadType) {
          this.batchUpload(_list);
        } else {
          this.batchUploadByPromiseAll(_list);
        }
        this.setData({
          imgUrlTempList: _list,
          isPending: true,
        })
      },
    });
  },
  // 批量上传 临时图片
  async batchUpload(_imgUrlTempList) {
    wx.showLoading();
    const START_TIME = new Date().getTime();
    const {
      length
    } = _imgUrlTempList;
    this.setData({
      maxVal: length
    })
    for (let index = 0; index < length; index++) {
      const temp = _imgUrlTempList[index];
      const type = temp.split('.')[temp.split('.').length - 1];
      try {
        const res = await wx.cloud.uploadFile({
          // 指定上传到的云路径
          cloudPath: `mobile-photos/${new Date().getTime()}-${index}.${type}`,
          // 指定要上传的文件的小程序临时文件路径
          filePath: temp,
          config: {
            // env: this.data.envId
          }
        });
        this.setData({
          curVal: index + 1
        })
        if (index === length - 1) {
          this.setData({
            curVal: length
          })
          wx.hideLoading()
          const DIFF_TIME = new Date().getTime() - START_TIME;
          const logItem = {
            id: `log${Math.random().toString(36).substr(2, 9)}`,
            count: length,
            time: DIFF_TIME / 1000
          }
          this.setData({
            logList: [logItem, ...this.data.logList]
          })
        }
      } catch (error) {
        console.log(e);
        wx.hideLoading();
        wx.showToast({
          title: '上传出错',
          icon: 'error'
        })
      }
    }
  },
  // 批量上传 Promise.all
  async batchUploadByPromiseAll(list) {
    wx.showLoading();
    const START_TIME = new Date().getTime();
    const allP = list.map((item, index) => {
      const type = item.split('.')[item.split('.').length - 1];
      return wx.cloud.uploadFile({
        // 指定上传到的云路径
        cloudPath: `mobile-photos/${new Date().getTime()}-${index}.${type}`,
        // 指定要上传的文件的小程序临时文件路径
        filePath: item,
        config: {
          // env: this.data.envId
        }
      });
    })
    try {
      const resList = await Promise.all(allP);
    wx.hideLoading()
    const DIFF_TIME = new Date().getTime() - START_TIME;
    const logItem = {
      id: `log${Math.random().toString(36).substr(2, 9)}`,
      count: resList.length,
      time: DIFF_TIME / 1000
    }
    this.setData({
      logList: [logItem, ...this.data.logList]
    })
    } catch (error) {
      console.log(e);
        wx.hideLoading();
        wx.showToast({
          title: '上传出错',
          icon: 'error'
        })
    }
  },
  radioChange(e) {
    this.setData({
      uploadType: e.detail.value
    })
  }
})